package com.szh.mysobproject.ui.adapter

import android.graphics.Color
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.text.HtmlCompat
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.szh.mysobproject.databinding.QaMsgListItemBinding
import com.szh.mysobproject.model.msg.QaMsg
import com.szh.mysobproject.util.setFixOnClickListener


class QaMsgAdapter(private val adapterDelegate: AdapterDelegate) :
    PagingDataAdapter<QaMsg.Content, QaMsgAdapter.QaMsgViewHolder>(
        QaMsgDiffCallback()
    ) {

    class QaMsgDiffCallback : DiffUtil.ItemCallback<QaMsg.Content>() {
        override fun areItemsTheSame(oldItem: QaMsg.Content, newItem: QaMsg.Content): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: QaMsg.Content, newItem: QaMsg.Content): Boolean {
            return oldItem == newItem
        }
    }

    inner class QaMsgViewHolder(val binding: QaMsgListItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onViewAttachedToWindow(holder: QaMsgViewHolder) {
        super.onViewAttachedToWindow(holder)
        adapterDelegate.onViewAttachedToWindow(holder)
    }

    override fun onBindViewHolder(holder: QaMsgViewHolder, position: Int) {
        val itemView = holder.itemView
        val binding = holder.binding
        val ivAvatar = binding.ivAvatar
        val cbNickName = binding.cbNickName
        val tvDesc = binding.tvDesc
        val tvReplyMsg = binding.tvReplyMsg
        val tvChildReplyMsg = binding.tvChildReplyMsg
        val context = itemView.context
        val item = getItem(position) ?: return
        itemView.setFixOnClickListener {
            adapterDelegate.onItemClick(it, position)
        }
        cbNickName.text = item.nickname
        ivAvatar.loadAvatar(false, item.avatar)
        tvDesc.text = item.timeText
        tvReplyMsg.height = 0
        val preText = "回答了朕的提问：「"
        val qaTitle = HtmlCompat.fromHtml(item.title, HtmlCompat.FROM_HTML_MODE_LEGACY)
        val suffixText = "」去看看问题解决了吗？"
        val ss = SpannableString(preText + qaTitle + suffixText)
        if (TextUtils.isEmpty(qaTitle).not()) {
            val highlightColor = Color.parseColor("#1D7DFA")
            val fcs = ForegroundColorSpan(highlightColor)
            val startIndex = preText.length
            val endIndex = preText.length + qaTitle.length
            ss.setSpan(fcs, startIndex, endIndex, SpannableString.SPAN_INCLUSIVE_INCLUSIVE)
        }
        tvChildReplyMsg.text = ss
        tvChildReplyMsg.ellipsize = TextUtils.TruncateAt.MIDDLE
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QaMsgViewHolder {
        val binding =
            QaMsgListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return QaMsgViewHolder(binding)
    }
}